import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ttq
 * Date: 13-8-8
 * Time: 下午4:06
 * To change this template use File | Settings | File Templates.
 */
public class CatDaoImpl extends HibernateDaoSupport implements ICatDao {
    @Override
    public void createCat(Cat cat) {
        this.getHibernateTemplate().persist(cat);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Cat> listCats() {
        return this.getHibernateTemplate().find(" select c from Cat c");
    }

    @Override
    public int getCatsCount() {
        Number n = (Number) this.getSession(true).createQuery(" select count(c) from Cat c").uniqueResult();
        return  n.intValue();
    }

    @Override
    public Cat findCatByName(String name) {
        List<Cat> catList = this.getHibernateTemplate().find("select c from Cat c where c.name = ? ", name);
        if (catList.size() > 0)
            return catList.get(0);
        return null;
    }

}
